#include "Macros.asm"

;--------------------------------------------------------------------
;
; This function handles the menu/UI for setting up a multiplayer
; game.  A game can have up to four players, but only 
; up to two calculator players.
;
;---------------------------------------------------------------------





Multiplayer_Lobby:

asm_flag1_0 .equ 0
asm_flag1_1 .equ 1
asm_flag1_2 .equ 2
asm_flag1_3 .equ 3
asm_flag1_4 .equ 4
asm_flag1_5 .equ 5

;This variable allows a player to input his name

Player_Input_Name .equ appbackupscreen + 701


;Although the variable Host or Join is used for connection
;purposes, this variable is used for name input purposes

Player_Number .equ appbackupscreen + 715

;Used for inputing names

Lobby_Cursor .equ appbackupscreen + 717


;For sending values between 2 calculators
Block_Of_Data .equ appbackupscreen + 720

Recieve_Successful .equ 123
Success .equ appbackupscreen + 740


;Because Get_Key rather than Get_CSC is used for inputing names,
;it is very important that the two calculators are synced.
;Thess values simply lets the other calculator know if the other
;player is in input mode

Falsey .equ 0
Truey .equ 1

Player_Me_In_Input_Name_Mode .equ appbackupscreen + 741
Player_They_In_Input_Name_Mode .equ appbackupscreen + 742


#define Player_Host_Or_Join asm_flag1_0

;If RES, Player is Host.  If SET, Player is joining.


#define Delete_Character_Flag asm_flag1_2

Flag .equ asm_flag1

; This is used for determining if the game is one player vs. AIs,
; or two players versus AIs
; If RES, it's a one player game.  If SET, it's a two player game.

#define AI_Or_Player2 asm_flag1_3

;Player_AI_Number is used by the menu only.  To save
;space, aka to allow functions to be called several
;times, this variable tracks whatever AI the player
;is editing difficulty for

Player_AI_Number .equ appbackupscreen + 700

;Also used for this sub only, for the same reasons, 
;in this case for determining if the AI difficulty
;is decreased or increased.

#define AI_Increase_Or_Decrease asm_flag1_1



;-----------For Debugging Purposes----------

 SET Player_Host_Or_Join, (IY + asm_flag1)
 SET AI_Or_Player2, (IY + Flag)

;-------------------------------------------
 



;For determining player types in gameplay

Host .equ 7  ;The person who hosted the game.  This will always be player 1.
Join .equ 8  ;The person joining the game in link play.  This will always be player 2.


;For determining player types in the lobby
Lobby_Host .equ 0
Lobby_Join .equ 1

AI_Very_Easy .equ 0
AI_Easy .equ 1
AI_Medium .equ 2
AI_Hard .equ 3
AI_Very_Hard .equ 4
AI_Insane .equ 5
AI_None .equ 6


;-----------------------------------------------------
;
; Box Width Values determine how long a selection box
; should be if the user selects a particular menu
; item
;
;-----------------------------------------------------

BoxWidthName .equ 57
BoxWidthTeam .equ 5
BoxWidthMapSelection .equ 95

BoxWidth .equ appbackupscreen + 2


 BIT AI_Or_Player2, (IY + Flag)
 JR Z, Player_Settings


 B_CALL _clrlcdfull
 LD HL, 1*256 + 3
 LD (curRow), HL
 LD HL, Connecting
 LD DE, OP1
 LD BC, 15
 LDIR
 LD HL, OP1
 B_CALL _PutS


Connect:

 CALL bell_connect
 jp z, Player_Settings

 

;Copy Default Player Names.  This might be used
;for debugging only.

Player_Settings:

 

 LD HL, Player1Name
 LD DE, Player1 + Player_Name
 LD BC, 14
 LDIR

 LD DE, Player2 + Player_Name
 LD BC, 14
 LDIR

 LD DE, Player3 + Player_Name
 LD BC, 14
 LDIR

 
 LD DE, Player4 + Player_Name
 LD BC, 14
 LDIR

 BIT AI_Or_Player2, (IY + Flag)
 CALL Z, Player_Is_AI

;This sets the default AIs to AI_Easy.


 LD A, 0 ;AI_None
 LD (Player3 + Player_Type_In_Game), A
 LD (Player4 + Player_Type_In_Game), A

 LD A, 49
 LD (Player1 + Player_Team), A
 LD A, 50
 LD (Player2 + Player_Team), A
 LD A, 51
 LD (Player3 + Player_Team), A
 LD A, 52
 LD (Player4 + Player_Team), A



;When the game loads, the default selection is Player One's
;name box.  The following code sets the correct box
;location and the fact that the box should be big enough
;to enclose the player's name.

 LD HL, 38*256 + 1
 LD (Player1 + Player_Screen_X),HL	;Sets X, Y positions,
					;refering to which item is
					;selected

 LD A, BoxWidthName
 LD (BoxWidth), A


;Draws the lobby

Multiplayer_Lobby_Draw:

 XOR A
 LD (Success), A

 
 call fastCopy
 RES fracDrawLFont, (IY + fontFlags)
 SET bufferOnly, (IY + plotFlag3)
 SET fullScrnDraw, (IY + apiFlg4)
 SET textWrite, (IY + sGrFlags)

 DrawLine(31,0,31,32,LineBlack)
 DrawLine(0,32,95,32,LineBlack)
 DrawLine(37,0,37,32,LineBlack)
 DrawLine(31,0,95,0,LineBlack)
 DrawLine(31,8,95,8,LineBlack)
 DrawLine(31,16,95,16,LineBlack)
 DrawLine(31,24,95,24,LineBlack)
 

 DrawSmallCharacter2(33,1, Player1 + Player_Team)
 DrawSmallCharacter2(33,9, Player2 + Player_Team)
 DrawSmallCharacter2(33,17,Player3 + Player_Team)
 DrawSmallCharacter2(33,25,Player4 + Player_Team)

 DrawSmallText(39, 1, Player1 + Player_Name)
 DrawSmallText(39, 9, Player2 + Player_Name)
 DrawSmallText(39, 17, Player3 + Player_Name)
 DrawSmallText(39, 25, Player4 + Player_Name)

 
 
Draw_Selection_Box:
 

 ;This is the code for drawing the box indicating where
 ;your cursor is located. 

 
 LD A, (Player1 + Player_Screen_Y)
 LD D, A
 LD A, (Player1 + Player_Screen_X)
 LD E,A
 ADD A, 6
 LD C, A
 LD A, (BoxWidth)
 ADD A, D
 LD B, A
 LD A, RectangleFilledInverse
 PUSH BC
 PUSH DE
 CALL _Rectangle_

 call fastCopyWithoutClear

;This is for when the player hits a key to either select an AI player,
;change a team, or change a map.

Lobby_Get_CSC:

  
 CALL Recieve_Data    ;This is when the calculator sees
			;if there is data to recieve and update
 LD A, (Success)
 OR A
 JP NZ, Multiplayer_Lobby_Draw
 B_CALL _GetCSC
 OR A
 JR Z, Lobby_Get_CSC

 
 CP skClear
 JP Z, Multiplayer_Lobby_Draw
 CP skDown
 JR Z, Move_Lobby_Menu_Down
 CP skUp
 JR Z, Move_Lobby_Menu_Up
 CP sk2nd
 JR Z, Check_Lobby_Context	;Check_Lobby_Context will take an action
				;depending on which option the player has
				;selected
 CP skEnter
 JR Z, Check_Lobby_Context
 CP skMode
 JP Z, Change_Team


;-----------------------------------------------------
;
; Moves the menu selection box
; down if the user presses the down
; key 
;
;-----------------------------------------------------

 


Move_Lobby_Menu_Down:
 
 POP DE
 POP BC
 LD A, RectangleFilledInverse
 CALL _Rectangle_
 LD A, (Player1 + Player_Screen_X)


 CP 33  ;(The point at which the menu highlighter cannot
	;cannot move down any more)
 JR Z, Draw_Selection_Box


 CP 25  ;(The point at which the person chooses to select
	;a map rather than a player)
 JR NZ, Move_Lobby_Menu_Down_2



 LD A, 0
 LD (Player1 + Player_Screen_Y), A
 ADD A, BoxWidthMapSelection
 LD (BoxWidth),A
 LD A, (Player1 + Player_Screen_X) 

Move_Lobby_Menu_Down_2:

 
 ADD A, 8
 LD (Player1 + Player_Screen_X), A

 
 JR Draw_Selection_Box


Move_Lobby_Menu_Up:
 
 POP DE
 POP BC
 LD A, RectangleFilledInverse
 CALL _Rectangle_
 LD A, (Player1 + Player_Screen_X)


 CP 1 	;(The point at which the menu highlighter cannot
	;cannot move up any more)
 JR Z,Draw_Selection_Box

 CP 33 ;(The point at which the person chooses to select
	;a player rather than a map)
 JR NZ, Move_Lobby_Menu_Up_2




 LD A, 38
 LD (Player1 + Player_Screen_Y), A
 LD A, BoxWidthName
 LD (BoxWidth), A

 LD A, (Player1 + Player_Screen_X) 

Move_Lobby_Menu_Up_2:

 SUB 8
 LD (Player1 + Player_Screen_X), A

 
 JP Draw_Selection_Box


;-----------------------------------------------------
; The next sub determines what to do when the enter
; key is pressed.  It is determined by whatever was
; highlighted when the enter key is pressed.
;-----------------------------------------------------


Check_Lobby_Context:
 
 
 LD A, (Player1 + Player_Screen_X)
 
 CP 1
 JP Z, Decide_If_Can_Change_Player_1_Name

 CP 9
 JP Z, Decide_Between_Join_and_AI
 
 CP 17
 JR Z, Select_AI_3

 CP 25
 JR Z, Select_AI_4


 JP Lobby_Get_CSC

;-----------------------------------------------------
;
; Determining an AI difficulty for Player 2
;
;-----------------------------------------------------


Select_AI_2:

 BIT Player_Host_Or_Join, (IY + Flag)
 JP NZ, Multiplayer_Lobby_Draw

 LD A, 1
 LD (Player_AI_Number), A


 CALL Select_AI_Box ;Draws the menu for AI selection

 DrawSmallText(23, 38, Player2 + Player_Name)
 
 call fastCopyWithoutClear

Select_AI_2_After_Draw:
 
 
 LD A, (Player2 + Player_Type_In_Game)
 LD H,0
 LD L, A

 LD D, H
 LD E, L
 PUSH DE


 call Select_AI_Get_Key
 jr Reset_AI

;-----------------------------------------------------
;
; Determining an AI difficulty for Player 3
;
;-----------------------------------------------------


Select_AI_3:

 BIT Player_Host_Or_Join, (IY + Flag)
 JP NZ, Multiplayer_Lobby_Draw


 LD A, 2
 LD (Player_AI_Number), A


 CALL Select_AI_Box ;Draws the menu for AI selection

 DrawSmallText(23, 38, Player3 + Player_Name)
 
 call fastCopyWithoutClear

Select_AI_3_After_Draw:
 
 
 LD A, (Player3 + Player_Type_In_Game)
 LD H,0
 LD L, A

 LD D, H
 LD E, L
 PUSH DE


 call Select_AI_Get_Key
 jr Reset_AI


;-----------------------------------------------------
;
; Determining an AI difficulty for Player 4
;
;-----------------------------------------------------

Select_AI_4:

 BIT Player_Host_Or_Join, (IY + Flag)
 JP NZ, Multiplayer_Lobby_Draw

 LD A, 3
 LD (Player_AI_Number), A


 CALL Select_AI_Box ;Draws the menu for AI selection

 DrawSmallText(23, 38, Player4 + Player_Name)
 
 call fastCopyWithoutClear

Select_AI_4_After_Draw:
 
 
 LD A, (Player4 + Player_Type_In_Game)
 LD H, 0
 LD L, A

 LD D, H
 LD E, L
 PUSH DE


 call Select_AI_Get_Key


















;-----------------------------------------------------
;
; Changes the AI if the user has selected a
; different one
;
;-----------------------------------------------------

Reset_AI:
 PUSH DE

 call fastCopyWithoutClear

 POP HL
 LD DE, 14
 SBC HL, DE
 EX DE, HL ;Saves location of name for later
 




 POP HL
 PUSH DE

 BIT AI_Increase_Or_Decrease, (IY + asm_flag1)
 JR Z, AI_Decrease_HL
 INC HL
 JR Pre_Multiply_By_62


AI_Decrease_HL:

 DEC HL


Pre_Multiply_By_62:

 ;Multiply by 62

 LD A, (Player_AI_Number)
 LD C, A
 LD B, 4
Multiply_By_31:
 ADD A, A
 ADD A, C
 DJNZ Multiply_By_31

 ADD A, A

 LD B, L  ;Saves AI Type to B to preserve the value

 LD HL, Player1
 LD D, 0
 LD E, A


 ADD HL, DE
 
 LD DE, Player_Type_In_Game

 ADD HL, DE

 


 LD (HL), B

 LD DE, 14
 SBC HL, DE
 POP DE
 EX DE, HL
 PUSH HL
 LD BC, 14
 LDIR
 
 POP HL
 
 
 LD A, (Player_AI_Number)
 ADD A, 10
 LD (Block_Of_Data), A
 
 LD DE, Block_Of_Data + 2
 LD BC, 14
 LDIR
 
  LD A, (Player_They_In_Input_Name_Mode)
  CP Truey
  CALL Z, bell_Sync

 CALL SendBlock
 LD A, (Player_AI_Number)

 CP 1
 JP Z, Select_AI_2_After_Draw
 CP 2
 JP Z, Select_AI_3_After_Draw
 CP 3
 JP Z, Select_AI_4_After_Draw


;-----------------------------------------------------
;
; The next sub is called to draw the menu
; for selecting an AI difficulty.
;
;-----------------------------------------------------


Select_AI_Box:  ;Draws the Box/Menu for selecting an AI Difficulty

 LD DE, 11*256+11
 LD BC, 84*256+54
 LD A, RectangleFilledWhite
 LD (LineColor), A
 CALL _Rectangle_

 LD DE, 11*256+11
 LD BC, 84*256+54
 LD A, RectangleOutlineBlack
 LD (LineColor), A
 CALL _Rectangle_

 LD DE, 13*256+13
 LD BC, 82*256 + 52
 LD A, RectangleOutlineBlack
 LD (LineColor), A
 CALL _Rectangle_
 
 DrawSmallText(32, 20, SelectAI)

 DrawSmallText(18, 38, SelectAIKeyLeft)
 DrawSmallText(76, 38, SelectAIKeyRight)
 
 RET



;-----------------------------------------------------
;
; The Player should either press left, right, 2nd,
; or Enter
;
;-----------------------------------------------------


Select_AI_Get_Key:
  
 PUSH HL

Select_AI_Get_Key_2:   ;Used to make sure the right value is popped
 


 ;CALL Recieve_Data
 
 B_CALL _GetCSC

 OR A
 JR Z, Select_AI_Get_Key_2
 
 CP skLeft
 JR Z, Decrease_AI_Difficulty
 CP skRight
 JR Z, Increase_AI_Difficulty
 CP skEnter
 JP Z, Multiplayer_Lobby_Draw 	;Go back to main
				;menu
 CP sk2nd
 JP Z, Multiplayer_Lobby_Draw
 
 JR Select_AI_Get_Key_2



;-----------------------------------------------------
;
; AI Functions
;
;-----------------------------------------------------





Increase_AI_Difficulty: 
 SET AI_Increase_Or_Decrease, (IY + asm_flag1)

 POP HL 
 LD A, L
 CP 6
 JR Z, Set_AI_Easy

 ;Multiply by 14, then add 14 to reach the correct AI


 CALL Multiply_By_14

 ADD HL, DE 
 

 LD DE, AI_Difficulty

 ADD HL, DE
 

 PUSH HL

 JR Display_AI_Type

Decrease_AI_Difficulty:

 RES AI_Increase_Or_Decrease, (IY + asm_flag1)

 POP HL
 LD A, L
 OR A
 JR Z, Set_AI_None

 ;Multiply by 14, then subtract 14 to reach the correct AI

 CALL Multiply_By_14
 
 SBC HL, DE

 LD DE, AI_Difficulty 
 ADD HL, DE

 PUSH HL


Display_AI_Type:

 LD hl,38*256+23
 LD (penCol),hl

 POP HL

 LD DE, OP1
 LD BC, 14
 LDIR
 PUSH HL
 LD HL, OP1
 B_CALL _VPutS
 POP DE

 
 
 RET
 
Set_AI_Easy:

 POP HL ;Clear Stack
 POP HL
 LD HL, -1
 LD DE, AI_Difficulty

 ;Set_AI_Exception is a sub used by both Set_AI_Easy
 ;and Set_AI_None, so to save space, it's put into
 ;another sub.  It's called Set_AI_Exception, 
 ;because there are two exceptions to the standard
 ;AI sub

 JR Set_AI_Exception 


Set_AI_None:

 POP HL
 POP HL
 LD HL, 7
 LD DE, AI_Difficulty + 84

Select_AI_Enter:

Set_AI_Exception:

 PUSH HL
 LD hl,38*256+23
 LD (penCol),hl
 
 LD H, D
 LD L, E 
 LD DE, OP1
 LD BC, 14
 LDIR
 PUSH HL
 LD HL, OP1
 B_CALL _VPutS
 POP DE
 JP Reset_AI


;-------------------------------------------------
; 
; For setting a player's name
;
;-------------------------------------------------


Edit_Player_Name_Host:
 
 LD HL, Player1 + Player_Name
 LD (Player_Number), HL
 JR Draw_Input_Name_Menu

Edit_Player_Name_Join:

 LD HL, Player2 + Player_Name
 LD (Player_Number), HL
 
Draw_Input_Name_Menu:
 
 LD A, Truey
 LD (Player_Me_In_Input_Name_Mode), A

 LD A, (Player_They_In_Input_Name_Mode)
 CP Truey
 CALL Z, bell_Sync
 ld hl, Player_They_In_Input_Name_Mode
 ld de, Player_Me_In_Input_Name_Mode
 CALL bell_swapBlock

 LD DE, 0*256+11
 LD BC, 95*256+54
 LD A, RectangleFilledWhite
 LD (LineColor), A
 CALL _Rectangle_

 LD DE, 0*256+11
 LD BC, 95*256+54
 LD A, RectangleOutlineBlack
 LD (LineColor), A
 CALL _Rectangle_



 LD DE, 2*256+13
 LD BC, 93*256 + 52
 LD A, RectangleOutlineBlack
 LD (LineColor), A
 CALL _Rectangle_

 DrawSmallText(29, 20, EnterName)
 
 SET fracDrawLFont, (IY + fontFlags)


 LD HL, 30*256+30
 LD (penCol), HL
  
 LD HL, (Player_Number)
 LD DE, Player_Input_Name
 LD BC, 14
 LDIR
 
 LD HL, 37*256+8
 LD (Lobby_Cursor), HL

Refresh_Input_Name_Menu:
 
 DrawSmallText(9,38, Player_Input_Name)
 RES Delete_Character_Flag, (IY + Flag)

DrawCursor:
 
 LD HL, (Lobby_Cursor)
 
 LD E, H
 LD D, L
 LD BC,6
 LD H, 45
 ADD HL, BC
  LD C, H
 LD B, L
 LD A, RectangleFilledInverse

 CALL _Rectangle_
  
 
 call fastCopyWithoutClear

;---------------------------------------
;
; This is the sub for actually entering
; a name for the player
;
;---------------------------------------

Hit_Character_Key:  
 SET lwrCaseActive, (IY + appLwrCaseFlag)
 
Data_Retrieve:
   

 CALL Recieve_Data
 JR Z, Data_Retrieve
 
 B_CALL _getkey


 CP kDel
 JP Z, Delete_Character

 CP kEnter
 JP Z, Change_Name

 CP 252 
 JR Z, Change_Character_To_Lowercase_Letter


 CP kSpace
 JR Z, Change_Character_To_Space


 CP $9A ;Capital A
 JR NC, Check_For_Capital_Letters

 CP $8E ;Number 0
 JR NC, Check_For_Numbers
 
 CP $8D
 JR Z, Change_Character_To_Period

 CP kClear
 JP Z, Clear_Name

 CP kLeft
 JR Z, Move_Name_Cursor_Left

 CP kRight
 JR Z, Move_Name_Cursor_Right

 JR Hit_Character_Key


Check_For_Capital_Letters:

 CP $B4
 JR C, Change_Character_To_Uppercase_Letter
 JR Hit_Character_Key

Check_For_Numbers:

 CP $98
 JR C, Change_Character_To_Number
 JR Hit_Character_Key

Change_Character_To_Period:

 LD A, Lperiod
 JR Change_Character

Change_Character_To_Uppercase_Letter:

 
 SUB $59
 JR Change_Character

Change_Character_To_Lowercase_Letter:

 LD HL, (keyextend)
 LD A, L
 SUB $81
 JR Change_Character

Change_Character_To_Number:

 SUB $5E
 JR Change_Character

Change_Character_To_Space:


 LD A, $20
 

Change_Character:
 PUSH AF
 ;First, erase the cursor
 CALL Erase_Cursor 


 POP AF
 LD HL, (Lobby_Cursor)
 LD DE, 6
 SBC HL, DE
 
 
;Divide by 6


 LD H, 0
 LD D, H
 LD E, L

 ADD HL, HL
 ADD HL, HL
 ADD HL, DE
 ADD HL, HL
 ADD HL, HL
 ADD HL, DE
 ADD HL, HL
 ADD HL, DE
 

 LD E, H
 LD D, 0


 LD HL, Player_Input_Name

 ADD HL, DE
 

 LD (HL), A

 LD HL, (Lobby_Cursor)
 LD A, L
 CP 80
 JP Z, Refresh_Input_Name_Menu
 BIT Delete_Character_Flag, (IY + Flag)
 CALL NZ, Adjust_Cursor_For_Deleting_Character
 LD DE, 6
 ADD HL, DE
 LD (Lobby_Cursor), HL

 
 JP Refresh_Input_Name_Menu

Move_Name_Cursor_Left:

 LD A, (Lobby_Cursor)
 
 
 CP 8
 JP Z, Hit_Character_Key
 PUSH AF
 CALL Erase_Cursor
 POP AF
 SUB 6
 LD (Lobby_Cursor),A
 JP Refresh_Input_Name_Menu

Move_Name_Cursor_Right:

 LD A, (Lobby_Cursor)

 CP 80
 JP Z, Hit_Character_Key
 PUSH AF
 CALL Erase_Cursor
 POP AF
 ADD A, 6
 LD (Lobby_Cursor),A
 JP Refresh_Input_Name_Menu




Erase_Cursor:
 
 LD HL, (Lobby_Cursor)
 
 LD E, H
 LD D, L
 LD BC,6
 LD H, 45
 ADD HL, BC
  LD C, H
 LD B, L
 LD A, RectangleFilledInverse
 
 CALL _Rectangle_

 RET

Change_Name:

 LD HL, (Player_Number)

 LD DE, Player_Input_Name
 EX DE, HL
 LD BC, 14
 LDIR
 LD HL, (Player_Number)
 LD DE, appbackupscreen
 SBC HL, DE
 LD A, L
 LD (Block_Of_Data), A

 LD HL, (Player_Number)
 LD DE, Block_Of_Data + 2
 LD BC, 14
 LDIR
   
 LD A, Truey
 LD (Player_Me_In_Input_Name_Mode), A

 LD A, (Player_They_In_Input_Name_Mode)
 CP Truey
 CALL Z, bell_Sync
 ld hl, Player_They_In_Input_Name_Mode
 ld de, Player_Me_In_Input_Name_Mode
 CALL bell_swapBlock

  LD A, (Player_They_In_Input_Name_Mode)
  CP Truey
  CALL Z, bell_Sync

 CALL SendBlock
 JP Multiplayer_Lobby_Draw



Delete_Character:

 LD A, $20
 SET Delete_Character_Flag, (IY + Flag)
 JP Change_Character



Adjust_Cursor_For_Deleting_Character:

 SUB 12
 CP -4
 CALL Z, Add_6
 LD L, A
 RET


Add_6:

 ADD A, 6
 RET


Clear_Name:

 CALL Erase_Cursor
 LD HL, 37*256+8
 LD (Lobby_Cursor), HL
 
 LD HL, EraseName
 LD DE, Player_Input_Name
 LD BC, 14
 LDIR
 
 JP Refresh_Input_Name_Menu


;-------------------------------------------
;
; For changing teams
;
;-------------------------------------------



Change_Team:

 



 LD A, (Player1 + Player_Screen_X)
 CP 1
 JR Z, Set_Team_Player_1
 CP 9
 JR Z, Set_Team_Player_2
 CP 17
 JR Z, Set_Team_Player_3
 CP 25
 JR Z, Set_Team_Player_4

 JP Multiplayer_Lobby_Draw 
 

Set_Team_Player_1:

 BIT Player_Host_Or_Join, (IY + Flag)
 JP NZ, Multiplayer_Lobby_Draw

 LD A, (Player1 + Player_Team)
 CALL Increase_Team
 LD (Player1 + Player_Team), A

 
 LD L, 1
 CALL Store_Team_To_Block_Of_Data
 JP Multiplayer_Lobby_Draw
 

Set_Team_Player_2:

 BIT AI_Or_Player2, (IY + Flag)
 JR Z, Team_Can_Be_Set

 BIT Player_Host_Or_Join, (IY + Flag)
 JP Z, Multiplayer_Lobby_Draw

Team_Can_Be_Set:
 LD A, (Player2 + Player_Team)
 CALL Increase_Team
 LD (Player2 + Player_Team), A


 LD L, 2
 CALL Store_Team_To_Block_Of_Data
 JP Multiplayer_Lobby_Draw

Set_Team_Player_3:

 BIT Player_Host_Or_Join, (IY + Flag)
 JP NZ, Multiplayer_Lobby_Draw
 LD A, (Player3 + Player_Team)
 CALL Increase_Team
 LD (Player3 + Player_Team), A

 LD L, 3
 CALL Store_Team_To_Block_Of_Data
 JP Multiplayer_Lobby_Draw

Set_Team_Player_4:

 BIT Player_Host_Or_Join, (IY + Flag)
 JP NZ, Multiplayer_Lobby_Draw
 LD A, (Player4 + Player_Team)
 CALL Increase_Team
 LD (Player4 + Player_Team), A
 

 LD L, 4
 CALL Store_Team_To_Block_Of_Data
 JP Multiplayer_Lobby_Draw

Increase_Team:

 CP 52
 JR Z, Reset_Team_To_Team_One
 ADD A, 1
 RET

Reset_Team_To_Team_One
 

 LD A, 49

 RET

Store_Team_To_Block_Of_Data:

 LD (Block_Of_Data + 2), A
 LD A, L
 LD (Block_Of_Data), A
 
   
  LD A, (Player_They_In_Input_Name_Mode)
  CP Truey
  CALL Z, bell_Sync

 CALL SendBlock

 RET


Decide_Between_Join_and_AI:

 BIT AI_Or_Player2, (IY + Flag)
 JR NZ, Decide_If_Can_Change_Player_2_Name
 JP Select_AI_2

Decide_If_Can_Change_Player_2_Name:

 BIT Player_Host_Or_Join, (IY + Flag)
 JP Z, Multiplayer_Lobby_Draw
 JP Edit_Player_Name_Join

Decide_If_Can_Change_Player_1_Name:

 BIT Player_Host_Or_Join, (IY + Flag)
 JP NZ, Multiplayer_Lobby_Draw
 JP Edit_Player_Name_Host
 
Player_Is_AI:

 LD HL, AI_Difficulty
 LD DE, Player2 + Player_Name
 LD BC, 14
 LDIR
 RET

;--------------------------------------------
;
; This function is used for updating menus
; depending on what a particulator player
; selects
;
;--------------------------------------------
























































Recieve_Data:
   

 ld hl, Block_Of_Data

 call bell_recvBlock
 ret nz

 ld a, Recieve_Successful
 ld (Success), a
 ld hl, Block_Of_Data
 ld a, (HL) 

 INC HL
 INC HL

 CP 1
 JR Z, Store_Player_1_Team
 CP 2
 JR Z, Store_Player_2_Team
 CP 3
 JR Z, Store_Player_3_Team
 CP 4
 JR Z, Store_Player_4_Team

 CP 43
 JR Z, Store_Player_1_Name
 CP 105
 JR Z, Store_Player_2_Name
 CP 12
 JR Z, Store_Player_3_Name
 CP 13
 JR Z, Store_Player_4_Name
 
Store_Player_1_Team:

 ld a, (HL)
 ld (Player1 + Player_Team), A
 RET

Store_Player_2_Team:

 ld a, (HL)
 ld (Player2 + Player_Team), A
 RET

Store_Player_3_Team:

 ld a, (HL)
 ld (Player3 + Player_Team), A
 RET

Store_Player_4_Team:

 ld a, (HL)
 ld (Player4 + Player_Team), A
 RET

Store_Player_1_Name:

 LD DE, Player1 + Player_Name
 LD BC, 14
 LDIR
 RET

Store_Player_2_Name:

 LD DE, Player2 + Player_Name
 LD BC, 14
 LDIR
 RET

Store_Player_3_Name:

 LD DE, Player3 + Player_Name
 LD BC, 14
 LDIR
 LD A, (HL)
 LD (Player3+ Player_Type_In_Game), A
 RET 

Store_Player_4_Name:

 LD DE, Player4 + Player_Name
 LD BC, 14
 LDIR
 LD A, (HL)
 LD (Player4+ Player_Type_In_Game), A
 RET 


SendBlock:
 
 LD HL, Block_Of_Data
 LD BC, 16
 CALL bell_sendBlock

 JR NZ, SendBlock
 RET






























EndMenu:

 bjump(_JForceCmdNoChar)


;------------------------------------------------------------
;-------------------DRAWING RECTANGLES-----------------------
;------------------------------------------------------------


_Rectangle_:
 	bit    2,a
       res    2,a
       ld     (LineColor),a
       jr     nz,RectFilled
       push   de
       push   bc
       ld     h,b
       ld     l,e
       call   DoLine        ;top,left to top right
       pop    hl
       pop    de
       push   hl
       push   de
       ld     h,d           ;top,left to bottom,left
       inc    e
       dec    l
       call   DoLine
       pop    de
       pop    hl
       push   de
       push   hl
       ld     d,h           ;top,right to bottom,right
       inc    e
       dec    l
       call   DoLine
       pop    hl
       pop    de
       ld     e,l           ;bottom,left to bottom,right
       jp     DoLine
RectFilled:
       ld     h,b
       ld     l,e
       push   de
       push   bc
       call   DoLine
       pop    bc
       pop    de
       inc    e
       ld     a,c
       cp     e
       jr     nc,RectFilled
       ret







;------------------------------------------------------------
;-------------------DRAWING LINES----------------------------
;------------------------------------------------------------

_Line_:

DoLine:       
	ld	a,1
	ld	(xdir),a
	ld	(ydir),a
	ld	a,d
	sub	h
	ld	b,a
	jp	nc,flokx
	neg
	ld	b,a
	ld	a,255
	ld	(xdir),a
flokx:
	ld	a,e
	sub	l
	ld	c,a
	jp	nc,floky
	neg
	ld	c,a
	ld	a,255
	ld	(ydir),a	
floky:
	ld	e,0	;error
	ld	a,b
	cp	c
	jp	c,fly
	ld	d,b	;resx
	inc	b
fllx:
	call	pixelonfl
	ld	a,(xdir)
	add	a,h
	ld	h,a
	ld	a,e
	add	a,c
	ld	e,a
	cp	d
	jp	c,flxok
	sub	d
	ld	e,a
	ld	a,(ydir)
	add	a,l
	ld	l,a
flxok:
	djnz	fllx
	ret
fly:
	ld	a,b
	ld	b,c
	ld	c,a
	ld	d,b
	inc	b
flly:
	call	pixelonfl
	ld	a,(ydir)
	add	a,l
	ld	l,a
	ld	a,e
	add	a,c
	ld	e,a
	cp	d
	jp	c,flyok
	sub	d
	ld	e,a
	ld	a,(xdir)
	add	a,h
	ld	h,a
flyok:
	djnz	flly
	ret

pixelonfl:
       push   hl
       push   de
       push   bc
       ex     de,hl
       ld     a,(LineColor)
       call   PointLineCont
       pop    bc
       pop    de
       pop    hl
       ret

PointLineCont:
       ld     c,a
       ld     a,d
       push   de
       inc    c
       dec    c
       jr     z,PointOff
       dec    c
       jr     z,PointOn

	call	GetPixel
	xor	(hl)
PointCont:
       ld     (hl),a
       pop    de                   ;D = X, E = Y
       push   af
       ld     a,d
       srl    a
       srl    a
       srl    a
       add    a,20h

       ld     a,80h
       add    a,e

       pop    af

       ret
PointOff:
       call   GetPixel
       cpl
       and    (hl)
       jr     PointCont
PointOn:
       call   GetPixel
       or     (hl)
       jr     PointCont

GetPixel:
                            ;A = X coord
                            ;E = y coord
       ld     d,0
       ld     h,d
       ld     l,e
       add    hl,de
       add    hl,de
       add    hl,hl
       add    hl,hl
       ld     de,plotSScreen
       add    hl,de
       ld     b,0
       ld     c,a
       and    7
       srl    c
       srl    c
       srl    c
       add    hl,bc
       ld     b,a
       inc    b
       ld     a,1
GetPixelLoop:
       rrca
       djnz   GetPixelLoop
	ret


Multiply_By_14:

 ADD HL, HL
 ADD HL, HL
 ADD HL, HL
 ADD HL, HL
 SBC HL, DE
 SBC HL, DE 

 LD DE, 14

 RET

SelectAIKeyLeft:

.db $cf,0

SelectAIKeyRight:

.db $05,0

SelectAI:
.db "Select AI", 0

Player1Name:

 .db "PLAYER 1     ",0

Player2Name:

 .db "PLAYER 2     ",0

Player3Name:

 .db "AI: Very Easy",0

Player4Name:

 .db "AI: Very Easy",0

EnterName:

 .db "Enter Name",0

AI_Difficulty:
 
 .db "AI: Very Easy",0
 .db "AI: Easy",6,6,6,6,6,0
 .db "AI: Medium",6,6,6,0
 .db "AI: Hard",6,6,6,6,6,0
 .db "AI: Very Hard",0
 .db "AI: Insane",6,6,6,0
 .db "AI: None",6,6,6,6,6,0	;For if a player doesn't want three or four players, or 						;if only a
 		;2-3 player map is chosen.  At least two players are required, though

EraseName:

 .db "             ",0

Connecting:
 .db "Connecting ...",0
